package com.aiyige.page.coupon.adapter;

import android.view.View;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.coupon.model.UserCoupon;
import com.aiyige.page.coupon.util.CouponUtil;
import com.aiyige.utils.DateFormatUtil;
import com.aiyige.utils.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UserCouponAdapter extends BaseQuickAdapter<UserCoupon, UserCouponAdapter.ViewHolder> {


    public UserCouponAdapter() {
        super(R.layout.user_coupon_item, new LinkedList<UserCoupon>());
    }

    @Override
    protected void convert(ViewHolder helper, UserCoupon item) {
        helper.bindData(item);
    }

    public class ViewHolder extends BaseViewHolder {
        @BindView(R.id.priceTv)
        TextView priceTv;
        @BindView(R.id.conditionTv)
        TextView conditionTv;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.useTimeTv)
        TextView useTimeTv;
        @BindView(R.id.actionBtn)
        TextView actionBtn;
        @BindView(R.id.scopeTv)
        TextView scopeTv;
        UserCoupon data;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.actionBtn);
        }

        public void bindData(UserCoupon item) {
            data = item;
            titleTv.setText(data.getCouponName());
            try {
                String useableStartTimeStr = DateFormatUtil.formatMS("yyyy.M.dd", data.getUseableStartTime());
                String useableEndTimeStr = DateFormatUtil.formatMS("yyyy.M.dd", data.getUseableEndTime());
                useTimeTv.setText(useableStartTimeStr + "-" + useableEndTimeStr);
            } catch (Exception e) {
                useTimeTv.setText("");
            }
            try {
                priceTv.setText(StringUtils.priceFormatEscapeRedundantTailZero(data.getCouponAmount()));
            } catch (Exception e) {
                priceTv.setText("");
            }
            try {
                switch (data.getCouponType()) {
                    case CouponUtil.COUPON_TYPE_FULL_REDUCE:
                        conditionTv.setText(StringUtils.getString(R.string.full_reduce_condition, StringUtils.priceFormatEscapeRedundantTailZero(data.getFullAmount())));
                        break;
                    default:
                        conditionTv.setText("");
                        break;
                }
            } catch (Exception e) {
                conditionTv.setText("");
            }
            scopeTv.setText(data.getScopeDesc());
        }
    }
}
